/**
 * 
 */
package calendarInterface2;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.plaf.BorderUIResource;

import calendarEventManager.CEMEvent;

import com.toedter.calendar.JDateChooser;


/**
 * @author Mohamed Ali Ayed
 * @version 1.0 
 * @since 2011-11-10
 * @version 1.1 
 * @since 2011-11-26
 * @version 1.2 
 * @since 2011-12-10
 * @version 1.3 
 * @since 2011-12-10
 */
@SuppressWarnings("serial")
public class FUpdateEvent extends JFrame{
	private JLabel lName;
	private JTextField tName;
	private JLabel lDescription;
	private JTextArea tDescription;
	private JLabel lStartTime;
	private JDateChooser jdcStartTime;
	private JLabel lEndTime;
	private JDateChooser jdcEndTime;
	private JButton bUpdateEvent;
	private JButton bDeleteEvent;
	private JButton bCancelAddEvent;
	private JPanel jpStartTime;
	private JComboBox<String> jcbStartTime;
	private Time timeStartTime;
	private JPanel jpEndTime;
	private JComboBox<String> jcbEndTime;
	private JPanel pUpdateEvent;
	private JPanel pButtons;
	private CEMEvent oEvent;
	
	
	/**
	 * http://www.rgagnon.com/javadetails/java-0225.html
	 */
	public FUpdateEvent(CEMEvent oEvent) {
		super();
		this.oEvent = oEvent;
		BuildFrame();
	}
	public void BuildFrame() {
		setTitle("Event");
		setSize(400,300); 
		setLocationRelativeTo(null); //center the window
		setResizable(false); //prevent resizable
		setVisible(true);
		this.setLayout(new GridBagLayout());
		pUpdateEvent= new JPanel();
		pButtons = new JPanel(new GridLayout(1,3));
		pUpdateEvent.setSize(400,300);
		buildPanel();
	}
	/**
	 * 
	 */
	@SuppressWarnings("deprecation")
	public void buildPanel() {
		pUpdateEvent.setLayout(new GridLayout(4, 2));
		
		lName = new JLabel("Name",JLabel.CENTER);
		tName = new JTextField(oEvent.getName());
		
		lDescription = new JLabel("Description",JLabel.CENTER);
		tDescription = new JTextArea(oEvent.getDescription());
		tDescription.setLineWrap(true);
		JScrollPane scrollPane = 
				new JScrollPane(tDescription,
						JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
						JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
			
		scrollPane.setBorder(BorderUIResource.getEtchedBorderUIResource());
		
		lStartTime = new JLabel("Start time",JLabel.CENTER);
		jpStartTime = new JPanel();
		jpStartTime.setLayout(new GridLayout(1, 2));
		jdcStartTime = new JDateChooser(oEvent.getStartingTime());
		jcbStartTime = new JComboBox<String>();
		fillJCBTime(jcbStartTime);
		jcbStartTime.setSelectedIndex((oEvent.getStartingTime().getHours()*2 + getMins(oEvent.getStartingTime().getMinutes())));
		jpStartTime.add(jdcStartTime);
		jpStartTime.add(jcbStartTime);

		lEndTime = new JLabel("End time",JLabel.CENTER);
		jpEndTime = new JPanel();
		jpEndTime.setLayout(new GridLayout(1, 2 ));
		jdcEndTime = new JDateChooser(oEvent.getEndingTime());
		jcbEndTime = new JComboBox<String>();
		fillJCBTime(jcbEndTime);
		jcbEndTime.setSelectedIndex((oEvent.getEndingTime().getHours()*2 + getMins(oEvent.getEndingTime().getMinutes())));
		jpEndTime.add(jdcEndTime);
		jpEndTime.add(jcbEndTime);
		
		bUpdateEvent = new JButton("Save");
		bUpdateEvent.addActionListener(new ActionUpdateEvent());
		bCancelAddEvent = new JButton("Cancel");
		bCancelAddEvent.addActionListener(new ActionHideFrameEvent());
		bDeleteEvent = new JButton("Delete");
		bDeleteEvent.addActionListener(new ActionDeleteEvent());
		
		 
		pUpdateEvent.add(lName);
		tName.setPreferredSize(new Dimension(190,50));
		pUpdateEvent.add(tName);
		
		pUpdateEvent.add(lDescription);
		pUpdateEvent.add(scrollPane);
		
		pUpdateEvent.add(lStartTime);
		pUpdateEvent.add(jpStartTime);
		
		pUpdateEvent.add(lEndTime);
		pUpdateEvent.add(jpEndTime);
		
		bUpdateEvent.setPreferredSize(new Dimension(127,50));
		pButtons.add(bUpdateEvent);
		pButtons.add(bDeleteEvent);
		pButtons.add(bCancelAddEvent);
		
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(0,0,0,0);
		
		c.gridx = 0;
		c.gridy = 0;
		this.add(pUpdateEvent,c);
		c.gridx = 0;
		c.gridy = 1;
		this.add(pButtons,c);
		
	}
	
	private int getMins(int minutes) {
		if(Double.valueOf(minutes)/Double.valueOf(60) >= 0.5) {
			return 1;
		}
		else
			return 0;
	}
	/*
	 * fill the chekbox (Time) with time between 00:00 and 23:30 incrementing the minutes with 30
	 */
	@SuppressWarnings("deprecation")
	private void fillJCBTime(JComboBox<String> jcbTime) {
		timeStartTime = new Time(0,0,0);
		jcbTime.addItem(timeStartTime.toString().substring(0, 5));
		for(int i=0;i<47;i++){
			timeStartTime.setMinutes(timeStartTime.getMinutes()+30);
			jcbTime.addItem(timeStartTime.toString().substring(0, 5));
		}
	}

	/*
	 * merge selected date with selected time and convert in timestamd
	 */
	
	public Timestamp mergeToTimeStamp (JDateChooser jdcDate, JComboBox<String> jcbTime){
		Calendar c =jdcDate.getCalendar();
		int minuteStartEvent = Integer.parseInt((String)jcbTime.getSelectedItem().toString().substring(3,5));
    	int hoursEvent = Integer.parseInt((String)jcbTime.getSelectedItem().toString().substring(0,2));
    	c.set(Calendar.HOUR_OF_DAY, hoursEvent);
    	c.set(Calendar.MINUTE, minuteStartEvent);
    	c.set(Calendar.SECOND, 0); 
    	c.set(Calendar.MILLISECOND, 0);
    	Timestamp tsStartEvent = new Timestamp(12) ;
    	tsStartEvent.setTime(c.getTime().getTime());
    	return tsStartEvent;
	}
	
	public String convertjdc (JDateChooser jdcDate){
		String time;
    	try {
    		 time = jdcDate.getDate().toString();
    	}
    	catch (java.lang.NullPointerException ex) { 
    		 time ="";
    	}
    	return time;
	}
	
	class ActionUpdateEvent implements ActionListener{

        @SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent e) {
        	    
        	if ((tName.getText().equalsIgnoreCase("")) || (tDescription.getText().equalsIgnoreCase(""))){
        		OPError messError = new OPError();
        		messError.showErrorWindow(messError.NO_DESC_OR_NAME_ENTERED, messError.ERROR_FRAME);
        	}
        	else if  ((convertjdc(jdcStartTime).equalsIgnoreCase("")) || (convertjdc(jdcEndTime).equalsIgnoreCase(""))){
        		OPError messError = new OPError();
        		messError.showErrorWindow(messError.NO_STARTDATE_OR_ENDDATE_ENTERED, messError.ERROR_FRAME);
        	}
        	else if (mergeToTimeStamp(jdcStartTime,jcbStartTime).getDate() != mergeToTimeStamp(jdcEndTime,jcbEndTime).getDate()) {
        		OPError messError = new OPError();
        		messError.showErrorWindow(messError.NO_STARTDATE_OR_ENDDATE_ENTERED, messError.ERROR_FRAME);
        	}
        	else if (mergeToTimeStamp(jdcStartTime,jcbStartTime).getHours() > mergeToTimeStamp(jdcEndTime,jcbEndTime).getHours()) {
        		OPError messError = new OPError();
        		messError.showErrorWindow(messError.END_TIME_BEFORE_START_TIME, messError.ERROR_FRAME);
        	}
        	else {
        		CalendarInterface.updateEvent(oEvent.getEventId(),tName.getText(),tDescription.getText(),mergeToTimeStamp(jdcStartTime,jcbStartTime),
        				mergeToTimeStamp(jdcEndTime,jcbEndTime), oEvent.getAgendaId());
        		CalendarInterface.hideFrameEventUpdate();
        	}     	
        }           
	}

	class ActionHideFrameEvent implements ActionListener{
        public void actionPerformed(ActionEvent e) {
        	CalendarInterface.hideFrameEventUpdate();
        }              
	}
	
	class ActionDeleteEvent implements ActionListener{
        public void actionPerformed(ActionEvent e) {
        	CalendarInterface.deleteEvent(oEvent.getEventId(), oEvent.getAgendaId());
        	CalendarInterface.hideFrameEventUpdate();
        }             
	}
}
